package com.itcpeng.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itcpeng.constant.MessageConstant;
import com.itcpeng.entity.PageResult;
import com.itcpeng.entity.QueryPageBean;
import com.itcpeng.entity.Result;
import com.itcpeng.pojo.Menu;
import com.itcpeng.pojo.User;
import com.itcpeng.service.MenuService;
import com.itcpeng.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/user")
public class UserController {

    @Autowired
    private BCryptPasswordEncoder encoder;

    @Reference
    private UserService userService;

    @Reference
    private MenuService menuService;


    //获取当前用户名
    @GetMapping(value = "/getUsername")
    public Result fetUsername() {
        /**
         * SecurityContextHolder():持有security所有信息
         * getAuthentication():获取认证信息   在获取用户名
         */
        String name = SecurityContextHolder.getContext().getAuthentication().getName();
//        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
//        String username = user.getUsername();
//        System.out.println(username);
        return new Result(true, MessageConstant.GET_USERNAME_SUCCESS, name);
    }

    // 查询菜单数据(需要根据登录用户, 动态获取菜单数据进行返回, 让前端页面进行显示)
    @RequestMapping(value = "/findMenuInfo", method = RequestMethod.POST)
    public Result findMenuInfo() {
        try {
            // 获取用户名
            String username = SecurityContextHolder.getContext().getAuthentication().getName();
            //查询菜单信息
            List<Menu> list = menuService.findMenuInfo(username);

            return new Result(true, MessageConstant.GET_MENU_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询图标列表失败!");
        }
    }



    //分页查询
    @RequestMapping("/findPage")
    private PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return userService.findPage(queryPageBean);
    }


    //添加用户
    @RequestMapping("/add")
    private Result add(@RequestBody User user, Integer[] roleIds) {
        try {
            user.setPassword(encoder.encode(user.getPassword()));
            //调用业务
            userService.add(user, roleIds);
            return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);
        }
    }


    //查询用户
    @RequestMapping("/findById")
    public Result findById(Integer id) {
        //调用业务，查询检查组
        User user = userService.findById(id);
        return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, user);
    }


    //根据检查组id查询与之关联的检查项id们
    @RequestMapping("/findRoleIdsByUserId")
    public List<Integer> findRoleIdsByUserId(Integer id) {

        List<Integer> ids = userService.findRoleIdsByUserId(id);
        return ids;
    }


    //编辑检查组
    @RequestMapping("/edit")
    public Result edit(@RequestBody User user, Integer[] roleIds) {
        try {
            //用户密码加盐
            user.setPassword(encoder.encode(user.getPassword()));
            userService.edit(user, roleIds);
            return new Result(true, "编辑用户成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "编辑用户失败");
        }
    }


    //删除检查组
    @RequestMapping("/delete")
    public Result delete(Integer id) {
        try {
            userService.delete(id);
            return new Result(true, "删除用户成功");
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "删除用户失败");
        }
    }
}
